package com.simon.battery.dao;

import java.sql.SQLException;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;
import com.simon.battery.model.BatteryModel;
import com.simon.battery.model.DisChargingModel;

public class BatteryCheckerDaoHelper extends OrmLiteSqliteOpenHelper {

	private static final String DATABASE_NAME = "batterychecker.db";
	private static final int DATABASE_VERSION = 4;
	private Dao<BatteryModel, Integer> batteryModelDao = null;
	private Dao<DisChargingModel, Integer> batteryDisChargingDao = null;
	private RuntimeExceptionDao<BatteryModel, Integer> batteryModelRuntimeDao = null;
	private RuntimeExceptionDao<DisChargingModel, Integer> batteryDisChargingRuntimeDao = null;

	public BatteryCheckerDaoHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	/**
	 * This is called when the database is first created. Usually you should
	 * call createTable statements here to create the tables that will store
	 * your data.
	 */
	@Override
	public void onCreate(SQLiteDatabase db, ConnectionSource connectionSource) {
		try {
			Log.i(BatteryCheckerDaoHelper.class.getName(), "onCreate");
			TableUtils.createTable(connectionSource, BatteryModel.class);
			TableUtils.createTable(connectionSource, DisChargingModel.class);
		} catch (SQLException e) {
			Log.e(BatteryCheckerDaoHelper.class.getName(),
					"Can't create database", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * This is called when your application is upgraded and it has a higher
	 * version number. This allows you to adjust the various data to match the
	 * new version number.
	 */
	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int oldVersion, int newVersion) {
		try {
			Log.i(BatteryCheckerDaoHelper.class.getName(), "onUpgrade");
			TableUtils.dropTable(connectionSource, BatteryModel.class, true);
			TableUtils
					.dropTable(connectionSource, DisChargingModel.class, true);
			// after we drop the old databases, we create the new ones
			onCreate(db, connectionSource);
		} catch (SQLException e) {
			Log.e(BatteryCheckerDaoHelper.class.getName(),
					"Can't drop databases", e);
			throw new RuntimeException(e);
		}
	}

	/**
	 * Returns the Database Access Object (DAO) for our SimpleData class. It
	 * will create it or just give the cached value.
	 */
	public Dao<BatteryModel, Integer> getBatteryCheckerDao()
			throws SQLException {
		if (batteryModelDao == null) {
			batteryModelDao = getDao(BatteryModel.class);
		}
		return batteryModelDao;
	}

	public Dao<DisChargingModel, Integer> getDisChargingBatteryDao()
			throws SQLException {
		if (batteryDisChargingDao == null) {
			batteryDisChargingDao = getDao(DisChargingModel.class);
		}
		return batteryDisChargingDao;
	}

	/**
	 * Returns the RuntimeExceptionDao (Database Access Object) version of a Dao
	 * for our SimpleData class. It will create it or just give the cached
	 * value. RuntimeExceptionDao only through RuntimeExceptions.
	 */
	public RuntimeExceptionDao<BatteryModel, Integer> getBatteryCheckerDataDao() {
		if (batteryModelRuntimeDao == null) {
			batteryModelRuntimeDao = getRuntimeExceptionDao(BatteryModel.class);
		}
		return batteryModelRuntimeDao;
	}

	public RuntimeExceptionDao<DisChargingModel, Integer> getDisChargingModelDao() {
		if (batteryDisChargingRuntimeDao == null) {
			batteryDisChargingRuntimeDao = getRuntimeExceptionDao(DisChargingModel.class);
		}
		return batteryDisChargingRuntimeDao;
	}

	/**
	 * Close the database connections and clear any cached DAOs.
	 */
	@Override
	public void close() {
		super.close();
		batteryModelDao = null;
		batteryDisChargingDao = null;
	}

}
